package f.app.al;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Budget extends Activity {
	ProgressBar pbFood,pbTransport,pbCloths,pbHobby;
	int food,transport,cloths,hobby,foodLeft;
	Database budgetDatabase;
	TextView cat1, cat2, cat3, cat4;
	String [] catList;
	
	SetBudget setBudget = new SetBudget();
	Entry entry = new Entry();
	Income lol = new Income();
	
	TextView viewFood,viewFoodSpent,viewTransport,viewTransportSpent,viewCloths,viewClothsSpent,viewHobby,viewHobbySpent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.budget);

		budgetDatabase = new Database(this);
		
		cat1 = (TextView) findViewById(R.id.txtFood);
		cat2 = (TextView) findViewById(R.id.txtTrans);
		cat3 = (TextView) findViewById(R.id.txtClothes);
		cat4 = (TextView) findViewById(R.id.txtHobby);
		
		pbFood = (ProgressBar) findViewById(R.id.pbFood);
		pbTransport = (ProgressBar) findViewById(R.id.pbTransport);
		pbCloths = (ProgressBar) findViewById(R.id.pbCloths);
		pbHobby = (ProgressBar) findViewById(R.id.pbHobby);
		
		viewFoodSpent = (TextView) findViewById(R.id.foodMoneySpent);
		viewFood = (TextView) findViewById(R.id.viewFoodleft);	
		viewTransport = (TextView) findViewById(R.id.viewTransport);
		viewTransportSpent = (TextView) findViewById(R.id.transportSpent);
		viewHobbySpent = (TextView) findViewById(R.id.viewHobby);
		viewHobby = (TextView) findViewById(R.id.viewHobbySpent);
		viewClothsSpent = (TextView) findViewById(R.id.viewCloths);
		viewCloths = (TextView) findViewById(R.id.viewClothsSpent);
		
		lol = budgetDatabase.getIncomeEntry();

		foodCalc((int)budgetDatabase.getCategorySum(0));
		transportCalc((int)budgetDatabase.getCategorySum(1));
		clothsCalc((int)budgetDatabase.getCategorySum(2));
		hobbyCalc((int)budgetDatabase.getCategorySum(3));
		catList = getResources().getStringArray(R.array.expenseList);
		
		  TextView cat1=(TextView)findViewById(R.id.txtFood);
		  cat1.setText(catList[0]);
		  TextView cat2=(TextView)findViewById(R.id.txtTrans);
		  cat2.setText(catList[1]);
		  TextView cat3=(TextView)findViewById(R.id.txtClothes);
		  cat3.setText(catList[2]);
		  TextView cat4=(TextView)findViewById(R.id.txtHobby);
		  cat4.setText(catList[3]);
	
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		budgetDatabase = new Database(this);
		lol = budgetDatabase.getIncomeEntry();

		foodCalc((int)budgetDatabase.getCategorySum(0));
		transportCalc((int)budgetDatabase.getCategorySum(1));
		clothsCalc((int)budgetDatabase.getCategorySum(2));
		hobbyCalc((int)budgetDatabase.getCategorySum(3));
	}
	
	private void foodCalc(int foodSpent){
		pbFood.setMax(lol.food);
		//pbFood.setMax(200);
		pbFood.setProgress(foodSpent);
		viewFoodSpent.setText(""+foodSpent+" kr");
		int foodLeft = pbFood.getMax()-foodSpent;
		viewFood.setText(""+foodLeft+" kr");
		if(pbFood.getMax()<foodSpent){
			viewFood.setTextColor(Color.RED);
		}
	}
	private void transportCalc(int transportSpent){
		pbTransport.setMax(lol.transport);
		pbTransport.setProgress(transportSpent);
		viewTransportSpent.setText(""+transportSpent+" kr");
		int transportLeft = pbTransport.getMax()-transportSpent;
		viewTransport.setText(""+transportLeft+" kr");
		if(pbTransport.getMax()<transportSpent){
			viewTransport.setTextColor(Color.RED);
		}
		
	}
	private void clothsCalc(int clothsSpent){
		pbCloths.setMax(lol.cloths);
		pbCloths.setProgress(clothsSpent);
		viewClothsSpent.setText(""+clothsSpent+" kr");
		int clothsLeft = pbCloths.getMax()-clothsSpent;
		viewCloths.setText(""+clothsLeft+" kr");
		if(pbCloths.getMax()<clothsSpent){
			viewCloths.setTextColor(Color.RED);
		}
	}
	private void hobbyCalc(int hobbySpent){
		pbHobby.setMax(lol.hobby);
		pbHobby.setProgress(hobbySpent);
		viewHobbySpent.setText(""+hobbySpent+" kr");
		int hobbyLeft = pbHobby.getMax()-hobbySpent;
		viewHobby.setText(""+hobbyLeft+" kr"); 
		if(pbHobby.getMax()<hobbySpent){
			viewHobby.setTextColor(Color.RED);
		}
	}
	
	
}
